package com.smart.upms.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.core.starter.utils.CopyUtils;
import com.smart.upms.constant.CacheConstant;
import com.smart.upms.sys.entity.SysDict;
import com.smart.upms.sys.mapper.SysDictMapper;
import com.smart.upms.sys.param.SysDictParam;
import com.smart.upms.sys.param.SysDictQueryParam;
import com.smart.upms.sys.result.SysDictResult;
import com.smart.upms.sys.service.ISysDictService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author guxiaobai
 * @since 2018-12-05
 */
@Service
@AllArgsConstructor
public class SysDictServiceImpl implements ISysDictService {

    private final SysDictMapper mapper;


    @Override
    @CacheEvict(value = CacheConstant.CacheDict.DICT_DETAILS, key = "#param.type")
    public Boolean save(SysDictParam param) {
        SysDict entity = CopyUtils.copyObject(param, SysDict.class);
        int insert = mapper.insert(entity);
        return insert > 0;
    }

    @Override
    @CacheEvict(value = CacheConstant.CacheDict.DICT_DETAILS, key = "#param.type")
    public Boolean update(SysDictParam param) {
        SysDict entity = CopyUtils.copyObject(param, SysDict.class);
        int update = mapper.updateById(entity);
        return update > 0;

    }

    @Override
    public SysDictResult get(Long id) {
        SysDict entity = mapper.selectById(id);
        return CopyUtils.copyObject(entity, SysDictResult.class);
    }

    @Override
    public List<SysDictResult> list(SysDictQueryParam param) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        List<SysDict> entityList = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(entityList, SysDictResult.class);
    }

    @Override
    public Page<SysDictResult> listPage(Page<SysDictResult> page, SysDictQueryParam param) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        if (param.getType() != null) {
            queryWrapper.eq(SysDict.TYPE, param.getType());
        }
        if (param.getStatus() != null) {
            queryWrapper.eq(SysDict.STATUS, param.getStatus());
        }
        Page<SysDict> entityPage = new Page<>();
        entityPage.setSize(page.getSize());
        entityPage.setCurrent(page.getCurrent());
        entityPage.setAsc(page.ascs());
        entityPage.setDesc(page.descs());
        mapper.selectPage(entityPage, queryWrapper);
        Page<SysDictResult> resultPage = CopyUtils.copyPage(entityPage, SysDictResult.class);
        return resultPage;
    }

    @Override
    @Cacheable(value = CacheConstant.CacheDict.DICT_DETAILS, key = "#type")
    public List<SysDictResult> listByType(String type) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysDict.TYPE, type);
        queryWrapper.orderByAsc(SysDict.SORT);
        List<SysDict> sysDicts = mapper.selectList(queryWrapper);
        return CopyUtils.copyList(sysDicts, SysDictResult.class);
    }

    @Override
    public Boolean removeById(Long id) {
        SysDict sysDict = mapper.selectById(id);
        if (sysDict != null) {
            return removeDict(sysDict);
        }
        return false;
    }


    @CacheEvict(value = CacheConstant.CacheDict.DICT_DETAILS, key = "#sysDict.type")
    public Boolean removeDict(SysDict sysDict) {
        int delete = mapper.deleteById(sysDict.getId());
        return delete > 0;
    }


}
